<template>
  <div>
    <my-bread :list="['权限管理','角色列表']"></my-bread>
    <el-card>
      <el-button type="primary" @click="openAddDialog()">添加角色</el-button>
      <el-table :data="rolesList" border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 权限数据  需要布局 -->
            <el-row
              style="border-bottom:1px solid #eee"
              :style="{ 'border-top': index === 0 ? '1px solid #eee' : 'none' }"
              :key="item.id"
              v-for="(item, index) in scope.row.children"
            >
              <el-col :span="4">
                <el-tag @close="delRights(scope.row, item.id)" closable>{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <el-row
                  :key="subItem.id"
                  v-for="(subItem, subIndex) in item.children"
                  :style="{
                    'border-top': subIndex !== 0 ? '1px solid #eee' : 'none'
                  }"
                >
                  <el-col :span="6">
                    <el-tag
                      @close="delRights(scope.row, subItem.id)"
                      closable
                      type="success"
                    >{{ subItem.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      @close="delRights(scope.row, lastItem.id)"
                      :key="lastItem.id"
                      v-for="lastItem in subItem.children"
                      closable
                      type="warning"
                    >{{ lastItem.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button icon="el-icon-edit" @click="openEditDialog(scope.row)" type="primary"></el-button>
              <el-button icon="el-icon-delete" @click="delRole(scope.row.id)" type="primary"></el-button>
              <el-button icon="el-icon-setting" @click="openRightsDialog(scope.row)" type="primary"></el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="添加角色" :visible.sync="addDialogFormVisible">
      <el-form :rules="Rules" :model="addForm" label-width="80px" autocomplete="off" ref="addForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑角色" :visible.sync="editDialogFormVisible">
      <el-form
        :rules="Rules"
        :model="editForm"
        label-width="80px"
        autocomplete="off"
        ref="editForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRole()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分配权限" :visible.sync="rightDialogFormVisible">
      <div>
        <el-tree
          :data="rightsTreeData"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys=checkedIdArr
          :props="defaultProps"
          ref="tree"
        ></el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rightDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="disRights()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "./Roles-Mixin";
export default {
  mixins: [mixin]
};
</script>

<style scoped>
.el-row {
  margin: 10px 0;
  display: flex;
  align-items: center;
}
.el-col {
  margin-right: 5px;
}
</style>
